package com.pktechan.manage.interceptor;


import com.pktechan.manage.entity.SysUser;
import com.pktechan.manage.utils.SessionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <br>
 * <b>功能：</b>控制用户权限的拦截器类<br>
 * <b>作者：</b>Wang Jiahao<br>
 * <b>日期：</b>April 7, 2017 <br>
 * <b>版权所有：<b>版权所有(C) 2017，高晟金服<br>
 */
public class SecurityInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(SecurityInterceptor.class);

    /**
     *
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取当前登录用户信息
        SysUser user = SessionUtils.getUser(request);
        if (user == null) {
            // 当前用户没有登录系统，返回错误信息，让用户回到登录页面
            LOGGER.error("用户没有登录系统。");
            response.sendRedirect("/logInController/login");
            return false;
        } else {
            LOGGER.error("用户已登录系统。");
            return true;
        }

    }
}
